import useGetUserInfo from "./useGetUserInfo";
import {useEffect} from "react";
import {useLocation, useNavigate} from "react-router-dom";
import {isLoginOrRegisterPath, isNoNeedUserInfoPath, LOGIN_PATH, MANAGE_INDEX_PATH} from "../router";

function useNavPage(waitingUserData: boolean) {
    const {pathname} = useLocation()
    const nav = useNavigate()

    const {username} = useGetUserInfo()
    useEffect(() => {
        if (waitingUserData) {
            return
        }
        if (username) {
            if (isLoginOrRegisterPath(pathname)) {
                nav(MANAGE_INDEX_PATH)
            }
            return;
        }
        if (isNoNeedUserInfoPath(pathname)) {
            return;
        } else {
            nav(LOGIN_PATH)
        }


    }, [waitingUserData, username, pathname])
}

export default useNavPage;